package actions;

import java.util.Vector;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import model.AdminControlPanel;

import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.actions.DispatchAction;

import beans.memberformbean;
import dto.JfMembers;
import dto.Sections;

public class UsersMngmntAction extends DispatchAction {
    

    AdminControlPanel admin ;
    Vector<JfMembers> members = new Vector<JfMembers>();
    Vector<JfMembers> moderator = new Vector<JfMembers>();
    Vector<Sections> sections = new Vector<Sections>();
    String memid;
    public UsersMngmntAction() {
    }
    
    public ActionForward getAllUsers(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {
        ActionForward forward= new ActionForward();
        System.out.println("entered");
        admin = new AdminControlPanel();
        try {
        	System.out.println("all inserted2222");
        	 members=admin.getAllUsers();
        	 System.out.println("all inserted");
            ((memberformbean)form).setMembers(members);
            
             forward = mapping.findForward("mem");
		} catch (Exception e) {
			e.printStackTrace();
			forward = mapping.findForward("faild");
		}
        
        return forward;
    }
    
    public ActionForward getAllModerator(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {
        ActionForward forward= new ActionForward();
        admin = new AdminControlPanel();
        try {
        	System.out.println("enter action");
        	 moderator=admin.getModerators();
        	 System.out.println("after retrive"); 
            ((memberformbean)form).setMembers(moderator);
            System.out.println("getall");
             forward = mapping.findForward("mod");
		} catch (Exception e) {
			forward = mapping.findForward("faild");
		}
        
        return forward;
    }
    
    public ActionForward demoteModerator(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {
         ActionForward forward= new ActionForward();
        String  mem ;
        mem = request.getParameter("moderator");
        Integer id =new Integer(mem);
        System.out.println(mem);
        admin = new AdminControlPanel();
        try {
        	 admin.demoteModerator(id);
        	 moderator=admin.getModerators();
            ((memberformbean)form).setMembers(moderator);
             forward = mapping.findForward("mod");
		} catch (Exception e) {
			forward = mapping.findForward("faild");
		}
        
        return forward;
    }
    public ActionForward promoteUserAsGlobal(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {
        ActionForward forward= new ActionForward();
        Integer type = new Integer(2);
        admin = new AdminControlPanel();
        String  mem ;
        mem = request.getParameter("memid");
        Integer id =new Integer(mem);
        try {
       	    admin.promoteUser(id,type,null);
            members=admin.getAllUsers();
        	 System.out.println("all inserted");
            ((memberformbean)form).setMembers(members);
         //  ((memberformbean)form).setMembers(moderator);
                 forward = mapping.findForward("mem");
		} catch (Exception e) {
			forward = mapping.findForward("faild");
                        e.printStackTrace();
		}
       
       return forward;
   }
    
    public ActionForward getAllSections(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {
        ActionForward forward= new ActionForward();
        memid = request.getParameter("memid");
        admin = new AdminControlPanel();
        try {
            sections=admin.getAllSections();
         ((memberformbean)form).setSections(sections);
            forward = mapping.findForward("promteMember");
		} catch (Exception e) {
			forward = mapping.findForward("faild");
		}
       
       return forward;
   }

    public ActionForward promoteUserAsLocal(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {
        ActionForward forward= new ActionForward();
        Integer type = new Integer(3);
        admin = new AdminControlPanel();
        String secid = request.getParameter("secid");
        Integer[] ids=((memberformbean)form).getSectIds();
        for(Integer id:ids){
            System.out.println("///////////////////// "+id);
        }
              
        try {
            
            System.out.println(new Integer(memid));
       	    admin.promoteUser(new Integer(memid),type,ids);
            System.out.println(memid);
            members=admin.getAllUsers();
        	 System.out.println("all inserted");
            ((memberformbean)form).setMembers(members);
         //  ((memberformbean)form).setMembers(moderator);
                 forward = mapping.findForward("mem");
		} catch (Exception e) {
			forward = mapping.findForward("faild");
                        e.printStackTrace();
		}

       return forward;
   }

    public ActionForward changeType(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {
        ActionForward forward= new ActionForward();
        String  mem ;
        mem = request.getParameter("moderator");
        Integer id =new Integer(mem);
        admin = new AdminControlPanel();
        try {
            sections=admin.getAllSections();
            ((memberformbean)form).setSections(sections);
            forward = mapping.findForward("promteMember");
		} catch (Exception e) {
			forward = mapping.findForward("faild");
		}

       return forward;
   }
    
    
    

}